//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.example.zwx.myapplication;

import android.util.Log;

public class IrLossyCompression {
    static final String TAG = IrLossyCompression.class.getSimpleName();
    static final int MAX_STATUS_TYPE_NUM = 15;
    byte[][] mOrigCode = new byte[15][4];
    byte codeNum;
    int thr;

    /**
     * constructor
     */
    IrLossyCompression() {
        this.resetIrCompressVal();
    }

    private void resetIrCompressVal() {
        this.thr = 2;
        this.codeNum = 0;

        for (int i = 0; i < 15; ++i) {
            for (int j = 0; j < 4; ++j) {
                this.mOrigCode[i][j] = 0;
            }
        }
    }

    private boolean irDataCompare(byte[] pA, byte pB0, byte pB1, byte pB2, byte pB3, int DeltaThr) {
        int IrSampleH1 = (pA[0] << 8 | pA[1] & 255) & '\uffff';
        int IrSampleL1 = (pA[2] << 8 | pA[3] & 255) & '\uffff';
        int IrSampleH2 = (pB0 << 8 | pB1 & 255) & '\uffff';
        int IrSampleL2 = (pB2 << 8 | pB3 & 255) & '\uffff';
        int IrDeltaH = IrSampleH1 - IrSampleH2;
        int IrDeltaL = IrSampleL1 - IrSampleL2;
        return IrDeltaH <= DeltaThr && IrDeltaH >= -DeltaThr && IrDeltaL <= DeltaThr && IrDeltaL >= -DeltaThr;
    }

    private int analysSingle(byte In0, byte In1, byte In2, byte In3) {
        if (In0 == 0 && In1 == 0) {
            return -3;
        } else if (this.codeNum == 0) {
            this.mOrigCode[0][0] = In0;
            this.mOrigCode[0][1] = In1;
            this.mOrigCode[0][2] = In2;
            this.mOrigCode[0][3] = In3;
            this.codeNum = 1;
            return 0;
        } else {
            for (int i = 0; i < this.codeNum; ++i) {
                boolean rev = this.irDataCompare(this.mOrigCode[i], In0, In1, In2, In3, this.thr);
                if (rev) {
                    return 0;
                }
            }

            if (this.codeNum < 15) {
                ++this.codeNum;
                this.mOrigCode[this.codeNum - 1][0] = In0;
                this.mOrigCode[this.codeNum - 1][1] = In1;
                this.mOrigCode[this.codeNum - 1][2] = In2;
                this.mOrigCode[this.codeNum - 1][3] = In3;
                return 0;
            } else {
                Log.i(TAG, "Err:in IrLossyCompression.class analysSingle( )");
                return -2;
            }
        }

    }

    private int analysPacket(byte[] pIn) {
        int len = pIn.length;
        byte[] IrIn = new byte[len - 6];

        int i;
        for (i = 0; i < len - 6; ++i) {
            IrIn[i] = (byte) (pIn[i + 6] & 255);
        }

        int size = len - 6 >> 2;
        int j = 0;

        for (i = 0; i < size; ++i) {
            int rev = this.analysSingle(IrIn[j], IrIn[j + 1], IrIn[j + 2], IrIn[j + 3]);
            j += 4;
            if (rev == -2) {
                Log.i(TAG, "Error code 1:  State value is more than 20, do not need to compress");
                return rev;
            }

            if (rev == -3) {
                Log.i(TAG, ",Error code 2:  Input code 0x00 0x00, not correct input");
                return rev;
            }
        }
        return 0;
    }

    private byte getIrEncoderSingle(byte input0, byte input1, byte input2, byte input3) {
        for (byte i = 0; i < this.codeNum; ++i) {
            boolean rv = this.irDataCompare(this.mOrigCode[i], input0, input1, input2, input3, this.thr);
            if (rv) {
                return i;
            }
        }

        return (byte) 15;
    }

    private byte[] getIrEncoder(byte[] input) {
        byte unComprssSize = 0;
        int insize = input.length;
        byte[] Pin = new byte[insize - 6];
        byte[] Pout = new byte[insize];
        byte[] PoutCp = new byte[insize];
        byte[] PoutUnCp = new byte[insize];
        short[] compresscnt = new short[2];
        boolean IrCnt = false;

        int i;
        for (i = 0; i < insize - 8; ++i) {
            Pin[i] = (byte) (input[i + 6] & 255);
        }

        int j = 0;
        int k = 0;
        compresscnt[0] = 0;
        compresscnt[1] = 0;

        for (i = 0; i < insize - 8; i += 4) {
            byte outenc = this.getIrEncoderSingle(Pin[i], Pin[i + 1], Pin[i + 2], Pin[i + 3]);
            if (!IrCnt) {
                IrCnt = true;
                PoutCp[j] = (byte) (outenc << 4);
                ++compresscnt[0];
            } else {
                IrCnt = false;
                PoutCp[j] |= outenc;
                ++j;
                ++compresscnt[0];
            }

            if (outenc == 15) {
                PoutUnCp[k] = Pin[i];
                PoutUnCp[k + 1] = Pin[i + 1];
                PoutUnCp[k + 2] = Pin[i + 2];
                PoutUnCp[k + 3] = Pin[i + 3];
                k += 4;
                unComprssSize = (byte) (unComprssSize + 4);
            }
        }

        Pout[0] = 120;
        Pout[1] = 86;
        Pout[2] = 5;
        Pout[3] = 38;
        Pout[4] = this.codeNum;
        byte var15 = 5;

        for (j = 0; j < this.codeNum; ++j) {
            if (j < 15 && 4 * j + var15 + 4 < Pout.length) {
                System.arraycopy(this.mOrigCode[j], 0, Pout, 4 * j + var15, 4);
            }
        }

        i = var15 + this.codeNum * 4;
        Pout[i] = (byte) (compresscnt[0] >> 8);
        Pout[i + 1] = (byte) compresscnt[0];
        i += 2;
        if (compresscnt[0] > PoutCp.length) {
            compresscnt[0] = (short) PoutCp.length;
        }

        if (compresscnt[0] > Pout.length - i) {
            compresscnt[0] = (short) (Pout.length - i);
        }

        System.arraycopy(PoutCp, 0, Pout, i, compresscnt[0]);
        if (compresscnt[0] % 2 != 0) {
            compresscnt[1] = (short) ((compresscnt[0] + 1) / 2);
        } else {
            compresscnt[1] = (short) (compresscnt[0] / 2);
        }

        i += compresscnt[1];
        if (unComprssSize > 0) {
            Pout[i] = unComprssSize;
            ++i;
            if (unComprssSize > PoutCp.length) {
                unComprssSize = (byte) PoutCp.length;
            }

            if (unComprssSize > Pout.length - i) {
                unComprssSize = (byte) (Pout.length - i);
            }

            System.arraycopy(PoutUnCp, 0, Pout, i, unComprssSize);
            i += unComprssSize;
        }

        Pout[i] = 0;
        Pout[i + 1] = 0;
        i += 2;
        byte[] CompressOut = new byte[i];
        if (i > Pout.length) {
            i = Pout.length;
        }

        if (i > CompressOut.length) {
            i = CompressOut.length;
        }

        System.arraycopy(Pout, 0, CompressOut, 0, i);
        return CompressOut;
    }

    public byte[] getIrEncoderFrame(byte[] inPutBuf) {
        if (inPutBuf.length < 10) {
            return null;
        } else if (inPutBuf[inPutBuf.length - 2] == 0 && inPutBuf[inPutBuf.length - 1] == 0) {
            this.resetIrCompressVal();
            if (inPutBuf[0] == 120 && inPutBuf[1] == 86 && inPutBuf[2] == 52 && inPutBuf[3] == 18) {
                int rev = this.analysPacket(inPutBuf);
                if (rev == 0) {
                    byte[] outPutBuf = this.getIrEncoder(inPutBuf);
                    return outPutBuf;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
